package ua.at.javac.dao;

import java.util.List;


import org.hibernate.Session;

import ua.at.javac.entity.City;
import ua.at.javac.entity.Journal;
import ua.at.javac.entity.Period;
import ua.at.javac.entity.Site;


public class Weather {
	// получаем таблицу средней погоды
	public List <Journal> getWeather (){
		Session session = HibernateUtil.getSessionFactory().openSession();
		@SuppressWarnings("unchecked")
		List <Journal> weatherTable = 
				session.createQuery("FROM Journal WHERE id=1780").list();
		System.out.println(weatherTable);
		return weatherTable;
	}
	
	// получаем таблицу городов
	public List <City> getCitys (){
		Session session = HibernateUtil.getSessionFactory().openSession();
		@SuppressWarnings("unchecked")
		List <City> siteCity = 
				session.createQuery("FROM City").list();
		return siteCity;
	}

	// получаем таблицу периодов
	public List <Period> getPeriods (){
		Session session = HibernateUtil.getSessionFactory().openSession();
		@SuppressWarnings("unchecked")
		List <Period> period = 
				session.createQuery("FROM Period").list();
		return period;
	}
	// получаем таблицу сайтов
	public List <Site> getSites (){
		Session session = HibernateUtil.getSessionFactory().openSession();
		@SuppressWarnings("unchecked")
		List <Site> site = 
				session.createQuery("FROM Site").list();
		return site;
	}

	public Long addWeather(Journal row) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Long id = (Long)session.save(row);
		session.getTransaction().commit();
		return id;
	}
	
	public List<Journal> getListWeatherDate(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		@SuppressWarnings("unchecked")
		List<Journal> journalDateList = session.createQuery("FROM Journal GROUP BY date").list();
		return journalDateList;
	}
	
	public void update (Journal row) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.saveOrUpdate(row);
		session.getTransaction().commit();
		
	}
}